package cn.edu.scau.cmi.yanweijie.client;
import cn.edu.scau.cmi.yanweijie.interProgramming.Marshal;
import cn.edu.scau.cmi.yanweijie.interProgramming.Chairman;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


import static javafx.application.Application.launch;

public class SingletonClient extends Application implements EventHandler<ActionEvent> {


    private String createButtonLine = "创建主席";

    private String EXIT = "Exit";

    private Chairman president = null ;

    int i=15,j=11;

    TextField textField = new TextField();
    TextField textField1 = new TextField();
    TextField textField2= new TextField();
    TextField textField3 = new TextField();

    Label dis1 = new Label();
    Label dis2 = new Label();
    Label dis3 = new Label();

    Button button1;
    Button button;

    GridPane pane = new GridPane();


    private String ERROR_MESSAGE = "单例模式只允许创建一个实例";



    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception{

        button = new Button("创建主席");
        button1 = new Button("创建元帅");
        button.setOnAction(this);
        button1.setOnAction(this);



        pane.add(textField,0,0);
        pane.add(button,15,0);
        pane.add(dis1,15,2);
        pane.add(dis2,15,3);



        pane.add(textField2,0,10);
        pane.add(button1,15,10);
/*
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);

        stackPane.getChildren().add(button1);*/

        Scene scene = new Scene(pane, 800, 800);

        primaryStage.setScene(scene);
        primaryStage.show();
    }


    @Override
    public void handle(ActionEvent event) {
        if (event.getSource() == button1) {
            System.out.println("你创建了元帅");

            Marshal marshal = Marshal.getInstance(textField2.getText());


            if (marshal.getName().equals(textField2.getText())) {
                pane.add(new Label("元帅："+textField2.getText()+marshal.toString()),i,j++);
            } else {
                pane.add(new Label("已经超过了十个元帅了，不能添加了"),i,j++);
            }
            textField2.setText("");

        }else{

            Chairman president = Chairman.getInstance(textField.getText());

            if (president.getName().trim().equals(textField.getText())) {
                dis1.autosize();
                dis1.setText(president.getName() + ":" + president.toString());
                dis2.setText("");
            } else {
                dis2.setText("已经创建过主席了，不能重复创建此对象");
            }
            System.out.println("你创建了主席");
        }
    }
}
